package com.onluy.plugins.telemanager;

import android.app.Activity;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import org.apache.cordova.CordovaPlugin;


public class VirtualLocation extends CordovaPlugin {

  private String mMockProviderName = LocationManager.GPS_PROVIDER;
  private Thread thread=null;
  private LocationManager locationManager;
  private Boolean is_run = true;

  private   String receiver;

  Activity activity;
  public  VirtualLocation (Activity activity){
    this.activity=activity;
  }


  private void setLocation(double longitude, double latitude) {
    Location location = new Location(mMockProviderName);
    location.setTime(System.currentTimeMillis());
    location.setLatitude(latitude);
    location.setLongitude(longitude);
    location.setAltitude(2.0f);
    location.setAccuracy(3.0f);
    location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
    locationManager.setTestProviderLocation(mMockProviderName, location);
  }

  private   void Init (){
    locationManager = (LocationManager) this.activity.getSystemService(this.activity.LOCATION_SERVICE);
    locationManager.addTestProvider(mMockProviderName, false, true, false, false, true, true, true, 0, 5);
    locationManager.setTestProviderEnabled(mMockProviderName, true);
    //locationManager.requestLocationUpdates(mMockProviderName, 0, 0,);
  }
  double longitude,latitude;
  public void StartThread( double _longitude, double _latitude) {
    // 开启线程，一直修改GPS坐标
    is_run=true;
    this.longitude=_longitude;
    this.latitude=_latitude;
    if(thread==null){
      Init ();
      thread = new Thread(new Runnable() {
        @Override
        public void run() {
          while (is_run) {
            try {
              Thread.sleep(50);
              setLocation( longitude,  latitude);
              Log.e("execute","====================设置信息 "+longitude);
            } catch (InterruptedException e) {
              Log.e("execute","====================设置信息异常 "+e.getMessage());
              e.printStackTrace();
            }
          }
        }
      });
      thread.start();
    }
  }


  private void StopThread( double longitude, double latitude) {
    is_run =false;
  }

  private Handler msgHandler;
  private  void  SendMsg(String msgstr){
    Log.e("===========", "SendMsg:" +1+ msgstr);
    if(msgHandler!=null) {
      Message msg = new Message();
      msg.what=1;
      msg.obj=msgstr;
      msgHandler.sendMessage(msg);//向Handler发送消息，
    }
  }

  public   void  SendCallBackMsg(){
      /*  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String msg="{\"cellNumber\":\""+getCellNumber()
                +"\",\"outNumber\":\""+getOutNumber()
                +"\",\"startDate\":\""+sdf.format(getStartDate())
                +"\",\"endDate\":\""+sdf.format(getEndDate())
                +"\",\"duration\":"+getDuration()
                +",\"receiver\":\""+getReceiver()+"\"}";
        if(getOutNumber().equals(getReceiver()))SendMsg(msg);*/
  }

  public void setMsgHandler(Handler msgHandler) {
    this.msgHandler = msgHandler;
  }

  public String getReceiver() {
    return receiver;
  }

  public void setReceiver(String receiver) {
    this.receiver = receiver;
  }
}
